package cn.hj.controler;

import cn.hj.beans.Person;
import cn.hj.mapper.PersonMapper;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MyController {
    @Autowired
    private Person person;
    @Autowired
    private MapperFactoryBean<PersonMapper>  mapper;
    @RequestMapping( "/hello/{id}")

    public String  hello(@PathVariable int id) throws Exception {
        Person person = mapper.getObject().getPersonById(id);
        System.out.println(person);
        return "hello";
    }

    @RequestMapping( "/insert")
    public String insertPerson() throws Exception {
        System.out.println(person);
        mapper.getObject().insertPerson(person);
        return "hello";
    }


    @RequestMapping("/all")
    @ResponseBody
    public String getAll() throws Exception {
        List<Person> allPerson = mapper.getObject().getAllPerson();
        return allPerson.toString();
    }

}
